package com.zl.demo.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zl.ble.BluetoothManger;
import com.zl.ble.ReceiveListener;
import com.zl.ble.util.StringUtil;
import com.zl.demo.R;
import com.zl.demo.config.AppConfig;
import com.zl.demo.util.ZViewUtil;

public class BleClientActivity extends AppCompatActivity {
    private EditText etAddress, etSend;
    private Button btnConnect, btnSend;
    private TextView tvReceive, tvSend;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble_demo);
        etAddress = findViewById(R.id.et_mac_address);
        etSend = findViewById(R.id.et_send);
        btnConnect = findViewById(R.id.btn_connect);
        btnSend = findViewById(R.id.btn_send);
        tvReceive = findViewById(R.id.tv_receive);
        tvSend = findViewById(R.id.tv_send);
        findViewById(R.id.lin_service).setVisibility(View.GONE);
        findViewById(R.id.lin_client).setVisibility(View.VISIBLE);
        btnConnect.setOnClickListener(view -> connectService());
        btnSend.setOnClickListener(view -> sendMsg());
        BluetoothManger.getInstance().setBluetoothType(false);
        BluetoothManger.getInstance().setReceiveListener(mReceiveListener);
        String address = AppConfig.getServiceMacAddress();
        if (!StringUtil.isEmpty(address)) {
            ZViewUtil.setTextViewText(etAddress, address);
        }
        String lastSend = AppConfig.getLastSendMsg();
        if (!StringUtil.isEmpty(lastSend)) {
            ZViewUtil.setTextViewText(etSend, lastSend);
        }
    }


    private void connectService() {
        String mac = ZViewUtil.getTextViewText(etAddress);
        if (!StringUtil.isEmpty(mac)) {
            BluetoothManger.getInstance().connectService(mac);
            AppConfig.setServiceMacAddress(mac);
        }
    }

    private void sendMsg() {
        String connect = ZViewUtil.getTextViewText(etSend);
        if (StringUtil.isEmpty(connect)) {
            return;
        }
//        byte[] msg = HexStringUtil.toBytes(connect);
        byte[] msg = connect.getBytes();
        BluetoothManger.getInstance().sendMessage(msg);
        showSendMsg(connect);
        AppConfig.setLastSendMsg(connect);
    }

    private ReceiveListener mReceiveListener = new ReceiveListener() {
        @Override
        public void onConnect() {
            ZViewUtil.viewRunUi(etAddress, () -> Toast.makeText(BleClientActivity.this, "连接成功", Toast.LENGTH_SHORT).show());
       }

        @Override
        public void onReceive(byte[] msg) {
            if (null != msg && msg.length > 0) {
//                showReceiveMsg(HexStringUtil.bytesToHex(msg));
                showReceiveMsg(new String(msg));
            }
        }

        @Override
        public void onReceiveLog(String msg) {

        }
    };


    public void showSendMsg(String msg) {
        if (null == tvSend || null == msg) return;
        String last = ZViewUtil.getTextViewText(tvSend);
        msg = last + "\n" + msg;
        ZViewUtil.setTextViewText(tvSend, msg);
    }

    public void showReceiveMsg(String msg) {
        if (null == tvReceive || null == msg) return;
        String last = ZViewUtil.getTextViewText(tvReceive);
        msg = last + "\n" + msg;
        ZViewUtil.setTextViewText(tvReceive, msg);
    }
}
